package com.yk.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.yk.serivice.PaymentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@DefaultProperties(defaultFallback = "gloable_Fallback_Handler")
public class PaymentController {
    final  PaymentService paymentService;

    @GetMapping("/8801/ok/{id}")
    @HystrixCommand
    public String paymentInfo_OK(@PathVariable Integer id) {
        //        int a = 1 / 0;
        String s = paymentService.paymentInfo_OK(id);
        return s;
    }

    @GetMapping("/8801/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOut_Handler",commandProperties ={
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    } )
    public String paymentInfo_TimeOut(@PathVariable Integer id) {

//        int a = 1 / 0;
        String s = paymentService.paymentInfo_TimeOut(id);
        return s;
    }

    public String paymentInfo_TimeOut_Handler(@PathVariable Integer id) {
        return "8801 请求失败或者时间太长,挂机了!" + id;
    }
    //gloable 服务降级

    public String gloable_Fallback_Handler() {
        return "全局处理异常1";
    }
}
